package com.oocl.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.oocl.factory.ServiceFactory;
import com.oocl.log4j.Log4j;
import com.oocl.pojo.Client;
import com.oocl.pojo.Merchandise;
import com.oocl.service.ClientService;
import com.oocl.vo.MerchandiseVO;

/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private ClientService clientService;   
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        clientService = ServiceFactory.getClientService();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("coming");
		String userName = request.getParameter("userName");
		String password = request.getParameter("password");
		Client client = clientService.validateLogin(userName, password);
		
		if(client != null){
			HttpSession session = request.getSession();
			session.setAttribute("client", client);
			Log4j.getLogger().info("{} login success ", client.toString());
			response.sendRedirect("index.jsp");
		}else{
			Log4j.getLogger().error("login failure, 不存在 此人");
			response.sendRedirect("login.jsp");
		}
	}

}
